package jp.ac.u_tokyo.dch.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.common.SeqenceGenerator;
import jp.ac.u_tokyo.dch.dao.data.DataDao;
import jp.ac.u_tokyo.dch.dao.data.DataDetailDao;
import jp.ac.u_tokyo.dch.dao.schema.DataSchemaDao;
import jp.ac.u_tokyo.dch.dao.schema.DataSchemaDetailDao;
import jp.ac.u_tokyo.dch.model.db.Data;
import jp.ac.u_tokyo.dch.model.db.DataDetail;
import jp.ac.u_tokyo.dch.model.db.DataSchema;
import jp.ac.u_tokyo.dch.model.db.DataSchemaDetail;
import jp.ac.u_tokyo.dch.util.common.DataInputUtil;
import jp.ac.u_tokyo.dch.view.Constants;
import au.com.bytecode.opencsv.CSVReader;

public class CsvUploadService {

	/**
	* DBコネクションオブジェクト
	 */
	private DBConnection dbconn = null;


	/**
	 * オブジェクト生成者
	 * @param dbconn
	 */
	public CsvUploadService(DBConnection dbconn){
		this.dbconn = dbconn;
	}

	public void insert(String schemaid, String oyaId, String user, String date, CSVReader csvReader) throws DBException, IOException {

		DataDao dataDao = new DataDao(dbconn);
		DataDetailDao dataDetailDao = new DataDetailDao(dbconn);
		DataSchemaDao dataSchemaDao = new DataSchemaDao(dbconn);
		DataSchemaDetailDao dataSchemaDetailDao = new DataSchemaDetailDao(dbconn);

		DataSchema dataSchema = dataSchemaDao.selectObject(schemaid);
		List<DataSchemaDetail> dataSchemaDetailList = dataSchemaDetailDao.selectList(schemaid);

		Map<String, DataSchemaDetail> detailMap = new HashMap<String, DataSchemaDetail>();

		for (DataSchemaDetail dataSchemaDetail : dataSchemaDetailList) {
			detailMap.put(dataSchemaDetail.getFieldKey(), dataSchemaDetail);
		}

		String sort = dataSchema.getSort();


		String[] fieldKeyInfo = csvReader.readNext();
		csvReader.readNext();
		String[] nextLine;

		Map<Integer, String> fieldIndexInfo = new HashMap<Integer, String>();

		int i = 0;
		for (String fieldKey : fieldKeyInfo) {

			fieldIndexInfo.put(i, fieldKey);
			i++;
		}

		String dataName = "";

		while ((nextLine = csvReader.readNext()) != null) {
			int index = 0;

			List<DataDetail> inputDataDetailList = new ArrayList<DataDetail>();

			for (String textData : nextLine) {

				String fieldKey = fieldIndexInfo.get(index);

				addList(fieldKey, textData, inputDataDetailList);

				if (isDataNameField(fieldKey, textData, sort)) {
					dataName = textData;
				}

				index++;
			}

			DataInputUtil.viewDataToDbData(dbconn, inputDataDetailList, detailMap);

			SeqenceGenerator seqenceGenerator = new SeqenceGenerator(dbconn);

			int dataId = seqenceGenerator.getSequence(SeqenceGenerator.DATA_SEQ);

			Data insertData = new Data();
			insertData.setName(dataName);
			insertData.setId(String.valueOf(dataId));
			insertData.setSchemaId(schemaid);
			insertData.setOyaId(oyaId);
			insertData.setSort(sort);
			insertData.setCreateUser(user);
			insertData.setModifyUser(user);
			insertData.setCreateDate(date);
			insertData.setModifyDate(date);

			dataDao.insert(insertData);
			dataDetailDao.insertAll(inputDataDetailList, dataId);
		}
	}

	private void addList(String fieldKey, String data, List<DataDetail> dataDetailList) {


		DataDetail detail = new DataDetail();
		detail.setFieldKey(fieldKey);

		String[] datas = {data};

		detail.setData(datas);

		dataDetailList.add(detail);

	}

	private boolean isDataNameField(String fieldKey, String data, String sort) {

		boolean check = false;

		if (Constants.LV4_SORT.equals(sort)) {
			if ("L4-011".equals(fieldKey)) {
				check = true;
			}
		} else if (Constants.LV5_SORT.equals(sort)) {
			if ("L5-0031".equals(fieldKey)) {
				check = true;
			}

		} else if (Constants.LV6_SORT.equals(sort)) {
			if ("L6-019".equals(fieldKey)) {
				check = true;
			}
		}
		return check;
	}

}
